﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace AlarmClock
{
    public class AlarmClock
    {
        private System.Windows.Forms.Timer timer; // 使用 System.Windows.Forms.Timer
        private DateTime alarmTime;
        private bool alarmSet = false;

        // 定义事件
        public event EventHandler Tick;
        public event EventHandler Alarm;

        // 构造函数
        public AlarmClock()
        {
            timer = new System.Windows.Forms.Timer();
            timer.Interval = 1000; // 每秒触发一次
            timer.Tick += OnTimedEvent;
        }

        // 启动时钟
        public void Start()
        {
            timer.Start();
        }


        // 设置闹钟时间
        public void SetAlarm(DateTime alarmTime)
        {
            this.alarmTime = alarmTime;
            alarmSet = true;
        }

        // 定时器每秒触发
        private void OnTimedEvent(object sender, EventArgs e)
        {
            // 触发Tick事件
            Tick?.Invoke(this, EventArgs.Empty);

            // 检查是否设置了闹钟，并且当前时间 >= 闹钟时间
            if (alarmSet && DateTime.Now >= alarmTime)
            {
                // 触发Alarm事件
                Alarm?.Invoke(this, EventArgs.Empty);
                alarmSet = false; // 重置闹钟状态，防止重复触发
            }
        }
    }
}

